﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class Admin_AdminLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //txtUserName.Text = string.Empty;
        txtUserName.Focus();
    }
    protected void btnLogin_Click(object sender, ImageClickEventArgs e)
    {
         MembershipUser userInfo = Membership.GetUser(txtUserName.Text);
            if (userInfo != null)
            {
                //validate user
                if (Membership.ValidateUser(txtUserName.Text.Trim(), txtPassword.Text.Trim()))
                {
                    FormsAuthentication.Initialize();
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, txtUserName.Text, DateTime.Now, DateTime.Now.AddMinutes(60), false, "1", FormsAuthentication.FormsCookiePath);
                    string hash = FormsAuthentication.Encrypt(ticket);
                    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash); // Hashed ticket

                    // Set the cookie's expiration time to the tickets expiration time
                    if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

                    // Add the cookie to the list for outgoing response
                    Response.Cookies.Add(cookie);
                    Response.Redirect("~/Admin/EditContracts.aspx");
                }
                else
                {
                    txtPassword.Focus();
                    lblError.Text = "Invalid password.";
                }
            }
        else 
        {
            txtUserName.Text = string.Empty;
            txtUserName.Focus();
            lblError.Text = "Invalid username.";
        }
    }
    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {

    }
}